package com.zhhtech.zhh.ui.helper

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.zhhtech.zhh.ui.ext.DefaultLifecycleObserver

typealias Receiver = (intent: Intent) -> Unit
typealias ReceiverBundle = Bundle.() -> Unit

fun ComponentActivity.localRegisterReceiver(action: String, receiver: Receiver) = localRegisterReceiver(this, this, arrayOf(action), receiver)
fun Fragment.localRegisterReceiver(action: String, receiver: Receiver) = localRegisterReceiver(context, this, arrayOf(action), receiver)
fun ComponentActivity.localRegisterReceiver(actions: Array<String>, receiver: Receiver) = localRegisterReceiver(this, this, actions, receiver)
fun Fragment.localRegisterReceiver(actions: Array<String>, receiver: Receiver) = localRegisterReceiver(context, this, actions, receiver)
fun ComponentActivity.localSendBroadcast(action: String, bundle: ReceiverBundle? = null) =
    localSendBroadcast(this, action, bundle?.let { Bundle().also(it) })

fun Fragment.localSendBroadcast(action: String, bundle: ReceiverBundle? = null) =
    localSendBroadcast(context, action, bundle?.let { Bundle().also(it) })

fun ComponentActivity.localSendBroadcastSync(action: String, bundle: ReceiverBundle? = null) =
    localSendBroadcastSync(this, action, bundle?.let { Bundle().also(it) })

fun Fragment.localSendBroadcastSync(action: String, bundle: ReceiverBundle? = null) =
    localSendBroadcastSync(context, action, bundle?.let { Bundle().also(it) })

fun localSendBroadcast(context: Context?, action: String, bundle: Bundle? = null) =
    context?.let { LocalBroadcastManager.getInstance(it).sendBroadcast(Intent(action).apply { bundle?.let { putExtras(it) } }) }

fun localSendBroadcastSync(context: Context?, action: String, bundle: Bundle? = null) =
    context?.let { LocalBroadcastManager.getInstance(it).sendBroadcastSync(Intent(action).apply { bundle?.let { putExtras(it) } }) }

fun localRegisterReceiver(context: Context?, owner: LifecycleOwner?, actions: Array<String>, receiver: Receiver) =
    BroadcastReceiverHelper(context, owner, actions, receiver)

class BroadcastReceiverHelper(
    private var context: Context? = null,
    private var mOwner: LifecycleOwner? = null,
    private val actions: Array<String>,
    private var receiver: Receiver? = null,
) : BroadcastReceiver(), DefaultLifecycleObserver {
    init {
        mOwner?.lifecycle?.addObserver(this)
        context?.let { LocalBroadcastManager.getInstance(it).registerReceiver(this, IntentFilter().apply { actions.forEach { addAction(it) } }) }
    }

    override fun onReceive(context: Context, intent: Intent) {
        if (actions.contains(intent.action)) receiver?.invoke(intent)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        mOwner?.lifecycle?.removeObserver(this)
        context?.let { LocalBroadcastManager.getInstance(it).unregisterReceiver(this) }
        receiver = null
        mOwner = null
        context = null
    }
}